<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class LevelLog extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        View::assign('level',Db::name('UserLevel')->order('rank asc')->column('level_name','rank'));
    }

    public function index(){
        //$map = $this->_search();

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('username')){
                $map['l.username'] = input('username');
            }
            if(input('level')){
                $map['l.new'] = input('level');
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['l.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('level_log')->alias('l')->where($map)->count();

            $field = 'l.*';
            $list = Db::name('level_log')
                ->alias('l')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            $level_arr = Db::name('user_level')->column('level_name','rank');
            foreach($list as $k=>$v){
                $v['old'] = $level_arr[$v['old']];
                $v['new'] = $level_arr[$v['new']];
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        $level_arr = Db::name('user_level')->field('level_name,rank')->select();
        View::assign('level_arr',$level_arr);
        return View::fetch();
    }

    public function edit(){
        if(IS_POST){
            $username = I('post.username');
            $rank = I('post.rank');
            if($username==''){
                return json(['status'=>0,'msg'=>'请填写会员手机号']);
            }
            $map = array('username'=>$username);
            $have = Db::name('Users')->where($map)->find();
            if(!$have){
                return json(['status'=>0,'msg'=>'您输入的会员不存在']);
            }

            if($have['jihuo']==0){
                return json(['status'=>0,'msg'=>'此会员未激活，不能升级']);
            }
            if($rank==$have['level']){
                return json(['status'=>0,'msg'=>'此会员已经是此级别，请重新选择']);
            }


            Db::startTrans();
            $insert = array();
            $insert['username'] = $username;
            $insert['old'] = $have['level'];
            $insert['new'] = $rank;
            $insert['w_time'] = getCurTime();
            $insert['admin_id'] = session('admin_id');
            if($rank>10) $day = tpCache('basic.jt_day'.$rank);
            else $day=0;
            $insert['day'] = $day;//记录拿奖天数
            $rs = Db::name('level_log')->insert($insert);
            if(!$rs){
                Db::rollback();
                return json(['status'=>0,'msg'=>'插入记录失败']);
            }

            $rs = Db::name('users')->where($map)->save(array('level'=>$rank));
            if(!$rs){
                Db::rollback();
                return json(['status'=>0,'msg'=>'更新会员级别失败']);
            }

            //进网
            if($have['is_vip']==0 && $rank>=10){
                $rs = logic('UsersLogic')->glNet($have,'平台调整级别'.yc_phone($have['username']).'进网',$rank);
                if(!$rs['status']){
                    Db::rollback();
                    return json(['status'=>0,'msg'=>$rs['msg']]);
                }
            }

            Db::commit();
            return json(['status'=>1,'msg'=>'修改成功']);
        }else{
            return View::fetch();
        }
    }

    public function del(){
        $id_arr = input('ids/a');
        Db::startTrans();
        Db::name('level_log')->where(['id'=>['in',$id_arr]])->delete();
        Db::commit();
        return json(['status'=>1,'msg'=>'删除成功!']);
    }
    public function excel(){

        $map = [];
        if(input('username')){
            $map['l.username'] = input('username');
        }
        if(input('level')){
            $map['l.new'] = input('level');
        }
        if(input('time_start') && input('time_end')){
            $time_start = strtotime(input('time_start'));
            $time_end = strtotime(input('time_end'));
            $map['l.w_time'] = ['between',[$time_start,$time_end]];
        }

        $field = 'l.*';
        $list = Db::name('level_log')
            ->alias('l')
            ->field($field)
            ->where($map)
            ->order('l.id desc ')
            ->select();
        $title = ['id','会员编号','原级别','新级别','备注','管理员ID','操作时间'];
        $level_arr = Db::name('user_level')->column('level_name','rank');
        $data = [];
        foreach($list as $k=>$v){
            $v['old'] = $level_arr[$v['old']];
            $v['new'] = $level_arr[$v['new']];
            $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            $arr = [
                $v['id'],$v['username'],$v['old'],$v['new'],$v['desc'],$v['admin_id'],$v['w_time']
            ];
            $data[] = $arr;
        }
        $this->export_excel($title,$data);
    }
}
